package com.haochang.chunkvod.app.database.order;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.VodApplication;
import com.haochang.chunkvod.app.utils.TimeUtils;
import com.haochang.chunkvod.model.order.GoodsInfo;
import com.haochang.chunkvod.model.order.RoomInfo;
import com.j256.ormlite.dao.RuntimeExceptionDao;

import android.content.Context;
import android.database.Cursor;

/**
 *@author qintao
 *created at 2016/6/15 17:37
 */

public class GoodsInfoDao extends OrderDaoManager<GoodsInfo>
{
	private static GoodsInfoDao dao;
	private final static Object gLock = new Object();

	private GoodsInfoDao(Context context)
	{
		super(context);
	}

	public static GoodsInfoDao getDao()
	{
		if (dao == null)
			dao = new GoodsInfoDao(VodApplication.appContext);
		return dao;
	}

	/**
	 * 更新购物车里面的商品数量或者创建
	 * 此时考虑两种情况，一种为增送模式，一种非增送模式，如果都是同一商品，两种模式的数量需分别进行展示
	 * @param item 没有口味时
	 */
	public boolean updateOrCreateFoodNum(GoodsInfo item)
	{
		boolean isSurplus = true;
		GoodsInfo info = GoodsInfoDao.getDao().queryBuilder(GoodsInfo.class, "bill_id", item.getBillId(), "goods_id", item.getGoodsId(), "menu_id",
				item.getMenuId(), "is_give", item.is_give(), "is_present", item.is_present(), "flavor", item.getFlavor());
		if (info != null)
		{
			if (info.getOrderNum() >= item.getSurplus())
			{
				isSurplus = false;
			} else
			{
				isSurplus = true;
				info.setOrderNum(info.getOrderNum() + 1);
				item.setOrderNum(info.getOrderNum());
				info.setOrderTime(TimeUtils.getNowTime());
				GoodsInfoDao.getDao().update(GoodsInfo.class, info);
			}
		} else
		{
			isSurplus = true;
			item.setOrderNum(1);
			item.setOrderTime(TimeUtils.getNowTime());
			GoodsInfoDao.getDao().insert(GoodsInfo.class, item);
		}
		return isSurplus;
	}

	/**
	 * 分组查询购物车里的商品信息
	 *
	 * @return
	 */
	public List<GoodsInfo> queryAllFoodInfos(String bill_id)
	{
		synchronized (gLock)
		{
			List<GoodsInfo> resultList = new ArrayList<GoodsInfo>();
			if (openHelper())
			{
				RuntimeExceptionDao<GoodsInfo, Integer> dao = this.openHelper.getRuntimeExceptionDao(GoodsInfo.class);
				if (dao != null)
				{
					StringBuffer strBuffer = new StringBuffer();
					strBuffer
							.append(" select goods_id,bill_id,menu_id,sum(order_num) as order_num ,goods_type,charging_room_type_id ,is_give,is_present from tb_goods where bill_id = '"
									+ bill_id + "'");
					strBuffer.append(" group by goods_id,bill_id,menu_id,is_give,is_present");
					Cursor cur = this.openHelper.getWritableDatabase().rawQuery(strBuffer.toString(), null);
					if (cur != null)
					{
						if (cur.getCount() > 0)
						{
							while (cur.moveToNext())
							{
								GoodsInfo info = new GoodsInfo();
								info.setGoodsId(cur.getString(0));
								info.setBillId(cur.getString(1));
								info.setMenuId(cur.getString(2));
								info.setOrderNum(cur.getInt(3));
								info.setGoods_type(cur.getInt(4));
								info.setCharging_room_type_id(cur.getString(5));
								info.setIs_give(cur.getInt(6) != 0);
								info.setIs_present(cur.getInt(7) != 0);
								resultList.add(info);
							}
						}
						if (!cur.isClosed())
							cur.close();
					}
				}
				closeHelper();
			}
			return resultList;
		}
	}

	/**
	 * 据账单id查询购物车品项数根
	 *
	 * @return
	 */
	public int queryGoodsInfoNum(String bill_id)
	{
		int num = 0;
		synchronized (gLock)
		{
			if (openHelper())
			{
				RuntimeExceptionDao<GoodsInfo, Integer> dao = this.openHelper.getRuntimeExceptionDao(GoodsInfo.class);
				if (dao != null)
				{
					StringBuffer strBuffer = new StringBuffer();
					strBuffer.append("select count(*) FROM tb_goods WHERE bill_id = '" + bill_id + "'");
					Cursor cur = this.openHelper.getWritableDatabase().rawQuery(strBuffer.toString(), null);
					if (cur != null)
					{
						if (cur.getCount() == 1)
						{
							while (cur.moveToNext())
							{
								num = cur.getInt(0);
							}
						}
						if (!cur.isClosed())
							cur.close();
					}
				}
				closeHelper();
			}
			return num;
		}
	}

	/**
	 * 分组查询购物车里的最小时间的商品信息
	 *
	 * @return
	 */
	public List<GoodsInfo> queryMinTimeGoodInfos()
	{
		synchronized (gLock)
		{
			List<GoodsInfo> resultList = new ArrayList<GoodsInfo>();
			if (openHelper())
			{
				RuntimeExceptionDao<GoodsInfo, Integer> dao = this.openHelper.getRuntimeExceptionDao(GoodsInfo.class);
				if (dao != null)
				{
					StringBuffer strBuffer = new StringBuffer();
					strBuffer.append(" select goods_id,bill_id,min(order_time) as order_time  from tb_goods group by bill_id ");
					Cursor cur = this.openHelper.getWritableDatabase().rawQuery(strBuffer.toString(), null);
					if (cur != null)
					{
						if (cur.getCount() > 0)
						{
							while (cur.moveToNext())
							{
								GoodsInfo info = new GoodsInfo();
								info.setGoodsId(cur.getString(0));
								info.setBillId(cur.getString(1));
								info.setOrderTime(cur.getString(2));
								resultList.add(info);
							}
						}
						if (!cur.isClosed())
							cur.close();
					}
				}
				closeHelper();
			}
			return resultList;
		}
	}

	/**
	 * 分组查询购物车里的商品信息
	 *
	 * @return
	 */
	public List<GoodsInfo> queryFoodInfos(String bill_id)
	{
		synchronized (gLock)
		{
			List<GoodsInfo> resultList = new ArrayList<GoodsInfo>();
			if (openHelper())
			{
				RuntimeExceptionDao<GoodsInfo, Integer> dao = this.openHelper.getRuntimeExceptionDao(GoodsInfo.class);
				if (dao != null)
				{
					StringBuffer strBuffer = new StringBuffer();
					strBuffer.append(" select goods_id,bill_id,menu_id,sum(order_num) as order_num,goods_name,unit  from tb_goods where bill_id = '" + bill_id
							+ "' group by goods_id ");
					Cursor cur = this.openHelper.getWritableDatabase().rawQuery(strBuffer.toString(), null);
					if (cur != null)
					{
						if (cur.getCount() > 0)
						{
							while (cur.moveToNext())
							{
								GoodsInfo info = new GoodsInfo();
								info.setGoodsId(cur.getString(0));
								info.setBillId(cur.getString(1));
								info.setMenuId(cur.getString(2));
								info.setOrderNum(cur.getInt(3));
								info.setGoodsName(cur.getString(4));
								info.setUnit(cur.getString(5));
								resultList.add(info);
							}
						}
						if (!cur.isClosed())
							cur.close();
					}
				}
				closeHelper();
			}
			return resultList;
		}
	}

	/**
	 * 分组查询每个房间购物车里的商品信息
	 *
	 * @return
	 */
	public List<RoomInfo> queryGoodInfosByBillId()
	{
		synchronized (gLock)
		{
			List<RoomInfo> list = new ArrayList<RoomInfo>();
			if (openHelper())
			{
				RuntimeExceptionDao<RoomInfo, Integer> dao = this.openHelper.getRuntimeExceptionDao(RoomInfo.class);
				if (dao != null)
				{
					StringBuffer strBuffer = new StringBuffer();
					strBuffer.append(" select bill_id,count(order_num) as order_num  from tb_goods  group by bill_id  ");
					Cursor cur = this.openHelper.getWritableDatabase().rawQuery(strBuffer.toString(), null);
					if (cur != null)
					{
						if (cur.getCount() > 0)
						{
							while (cur.moveToNext())
							{
								RoomInfo info = new RoomInfo();
								info.setBillId(cur.getString(0));
								info.setOrderNum(cur.getInt(1));
								list.add(info);
							}
						}
						if (!cur.isClosed())
							cur.close();
					}
				}
				closeHelper();
			}
			return list;
		}
	}
}
